package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;


import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j

//vip策略实现类
public class VipOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        //获取付款类型
        Long itemId = tradeVo.getItemId();
        String itemType = tradeVo.getItemType();
        Result<VipServiceConfig> vipServiceConfigResult = vipServiceConfigFeignClient.getVipServiceConfig(itemId);
        VipServiceConfig vipServiceConfig = vipServiceConfigResult.getData();

        //vip服务配置表
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setItemType(itemType);
        orderInfoVo.setOriginalAmount(vipServiceConfig.getPrice());
        orderInfoVo.setDerateAmount(vipServiceConfig.getPrice().subtract(vipServiceConfig.getDiscountPrice()));
        orderInfoVo.setOrderAmount(vipServiceConfig.getDiscountPrice());


        //订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName("vip会员"+vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(vipServiceConfig.getDiscountPrice());
        List<OrderDetailVo> orderDetailVoList = Arrays.asList(orderDetailVo);
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        //添加订单减免明细
        List<OrderDerateVo> orderDerateVoList = new ArrayList<OrderDerateVo>();
        if (orderInfoVo.getDerateAmount().doubleValue() >=0){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(orderInfoVo.getDerateAmount());
            orderDerateVoList.add(orderDerateVo);
        }
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        // 交易号数据，使用uuid ，后期应用于防止表单重复提交功能
        String tradeNo = UUID.randomUUID().toString().replaceAll("-","");
        redisTemplate.opsForValue().set(RedisConstant.ORDER_TRADE_NO + tradeNo,"1",5, TimeUnit.MINUTES);
        orderInfoVo.setTradeNo(tradeNo);
        orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);// 设置支付方式默认为微信支付
        orderInfoVo.setTimestamp(SignHelper.getTimestamp());
        Map<String , Object> signMap = JSON.parseObject(JSON.toJSONString(orderInfoVo), Map.class);
        orderInfoVo.setSign(SignHelper.getSign(signMap));


        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}
